package com.yimi.yinhepda.adapter.RecycleAdapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.pda.ym.base.CommonViewHolder;
import com.yimi.yinhepda.db.table.DepartmentTable;

import java.util.List;

/**
 * 出发线路分页适配器
 *
 * @author lj
 */
public class SetOutLinePageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    /**
     * 普通布局
     */
    private static final int TYPE_ITEM = 1;
    /**
     * 脚布局
     */
    private static final int TYPE_FOOTER = 2;
    /**
     * 当前加载状态，默认为加载完成
     */
    private int loadState = 2;
    /**
     * 正在加载
     */
    public static final int LOADING = 1;
    /**
     * 加载完成
     */
    public static final int LOADING_COMPLETE = 2;
    /**
     * 加载到底
     */
    public static final int LOADING_END = 3;
    /**
     * 加载失败
     */
    public static final int LOADING_ERROR = 4;
    private CommonViewHolder.onItemCommonClickListener commonClickListener;
    private List<DepartmentTable> dataList;

    public SetOutLinePageAdapter(List<DepartmentTable> dataList, CommonViewHolder.onItemCommonClickListener commonClickListener) {
        this.commonClickListener = commonClickListener;
        this.dataList = dataList;
    }

    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为FooterView
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size() + 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 通过判断显示类型，来创建不同的View
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.adapter_set_out_line_item, parent, false);
            return new RecyclerViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_refresh_footer, parent, false);
            return new FootViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RecyclerViewHolder) {
            RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
            recyclerViewHolder.tvDeptName.setText(dataList.get(position).getDeptName());
            recyclerViewHolder.tvDeptCode.setText(dataList.get(position).getDeptCode());
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            switch (loadState) {
                case LOADING:
                    // 正在加载
                    footViewHolder.pbLoading.setVisibility(View.VISIBLE);
                    footViewHolder.tvLoading.setVisibility(View.VISIBLE);
                    footViewHolder.llEnd.setVisibility(View.GONE);
                    break;
                case LOADING_COMPLETE:
                    // 加载完成
                    footViewHolder.pbLoading.setVisibility(View.GONE);
                    footViewHolder.tvLoading.setVisibility(View.GONE);
                    footViewHolder.llEnd.setVisibility(View.GONE);
                    break;
                case LOADING_END:
                    // 加载到底
                    footViewHolder.pbLoading.setVisibility(View.GONE);
                    footViewHolder.tvLoading.setVisibility(View.GONE);
                    footViewHolder.llEnd.setVisibility(View.VISIBLE);
                    break;
                case LOADING_ERROR:
                    //加载失败
                    footViewHolder.pbLoading.setVisibility(View.INVISIBLE);
                    footViewHolder.tvLoading.setVisibility(View.VISIBLE);
                    footViewHolder.tvLoading.setText("加载失败");
                    footViewHolder.llEnd.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 设置上拉加载状态
     *
     * @param loadState 0.正在加载 1.加载完成 2.加载到底
     */
    public void setLoadState(int loadState) {
        this.loadState = loadState;
        notifyDataSetChanged();
    }

    private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {

        private TextView tvDeptName;
        private TextView tvDeptCode;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            tvDeptName = itemView.findViewById(R.id.tv_next_dept_name);
            tvDeptCode = itemView.findViewById(R.id.tv_next_dept_code);
        }


        @Override
        public void onClick(View v) {
            if (commonClickListener != null) {
                commonClickListener.onItemClickListener(getAdapterPosition());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (commonClickListener != null) {
                commonClickListener.onItemLongClickListener(getAdapterPosition());
            }
            return false;
        }
    }

    private class FootViewHolder extends RecyclerView.ViewHolder {

        ProgressBar pbLoading;
        TextView tvLoading;
        LinearLayout llEnd;

        FootViewHolder(View itemView) {
            super(itemView);
            pbLoading = itemView.findViewById(R.id.pb_loading);
            tvLoading = itemView.findViewById(R.id.tv_loading);
            llEnd = itemView.findViewById(R.id.ll_end);
        }
    }
}
